﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fluq
{
	public static class DependencyComposerExtensions
	{
		public static object Compose(this IDependencyComposer source, IDependencyContainer container, Type type)
		{
			return Compose(source, container, type, Enumerable.Empty<IDependencyParameter>());
		}

		public static object Compose(this IDependencyComposer source, IDependencyContainer container, Type type, IEnumerable<IDependencyParameter> parameters)
		{
			if (source == null)
			{
				throw new ArgumentNullException("source");
			}

			object instance;

			if (!source.TryCompose(container, type, parameters, out instance))
			{
				throw new Exception(String.Format("Could not create an instance of {0}", type));
			}
			else
			{
				return instance;
			}
		}

		public static void Destroy(this IDependencyComposer source, object instance)
		{
			source.TryDestroy(instance);
		}
	}
}
